#include "SoundManager.h"

SoundManager* SoundManager::_instance = NULL;

SoundManager::SoundManager(void) {
	if (_instance != NULL) {
		CCLog("SoundManager :: this is a singleton class, please access via getInstance() function");
		return;
	}
	_isOn = true;
}

SoundManager::~SoundManager(void) {
	if (_instance) {
		_instance->release();
	}
}

SoundManager* SoundManager::getInstance() {
	if (!_instance) {
		_instance = new SoundManager();
	}
	_instance->retain();
	return _instance;
}

bool SoundManager::isEnabled() {
	return _isOn;
}

//set sound enabled or not
void SoundManager::setEnabled(bool value) {
	if (value == true) {
        if (_isOn == true) {
            return;
        }
        else{
            SimpleAudioEngine::sharedEngine()->resumeAllEffects();
            SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
        }
    } else {
        SimpleAudioEngine::sharedEngine()->pauseAllEffects();
        SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
    }
    this->_isOn = value;
}

void SoundManager::playEffectSound(std::string fileName) {
	CCLog("SoundManager::playEffectSound - fileName: %s", fileName.c_str());
	if (this->_isOn) {
		SimpleAudioEngine::sharedEngine()->stopAllEffects();
		SimpleAudioEngine::sharedEngine()->playEffect(fileName.c_str());
	}
}

void SoundManager::playBackgroundSound(std::string fileName) {
	CCLog("SoundManager::playBackgroundSound - fileName: %s", fileName.c_str());
	if (this->_isOn) {
		SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
		SimpleAudioEngine::sharedEngine()->playBackgroundMusic(fileName.c_str(), true);
	}
}
